package com.atguigu.queue;

import java.security.PublicKey;
import java.util.Scanner;

/**
 * @author shkstart
 * @create 2021-08-07 18:58
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        ArrayQueue queue = new ArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        char key=' ';
        boolean loop=true;
        //输出一个菜单
        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            //接受一个字符
            key=scanner.next().charAt(0);
            switch (key){
                case 's':queue.showQueue();
                    break;
                case 'a':
                    try {
                        System.out.println("请输入一个数据");
                        int value=scanner.nextInt();
                        queue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    //取出数据

                    try {
                        int n = queue.getQueue();
                        System.out.println("取出的数据："+n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    //取出头部的数据
                    try {
                        int res = queue.headQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    System.out.println("退出程序！");
                    break;
            }

        }


    }
}
    //使用数组模拟数列
   class ArrayQueue{
        private int maxSize;
        private int rear;
        private int front;
        private int [] arr;

        //创建构造器
        ArrayQueue(int arrMaxSize) {
            maxSize=arrMaxSize;
            rear=-1;
            front=-1;
            arr = new int[maxSize];
        }
        //判断是否为空
        public boolean isEmpty(){
            return rear==front;
        }
        //判断是否为满
        public boolean isFull(){
            return rear==maxSize-1;
        }
        //添加数据入队列
        public void addQueue(int n){
            if (isFull()){
                System.out.println("队列已经满！");
                return;
            }
            rear++;
            arr[rear]=n;

        }
        //让队列出数据
        public int getQueue(){
            if (isEmpty()){
                throw new RuntimeException("队列为空，没有数据");
            }
            front++;
            return arr[front];
        }

        //显示队列的数据
        public void showQueue(){
            if (isEmpty()){
                System.out.println("队列空的，没有数据~~");
                return;
            }
            for (int i=0;i<arr.length;i++){
                System.out.println("arr["+i+"]"+"="+arr[i]);
            }
        }
        //显示队列的头部数据
        public int headQueue(){
            if (isEmpty()){
                throw new RuntimeException("队列为空，没有数据！");
            }
            return arr[front+1];
        }

    }

